package com.ds.dsm.view.nav.service.tree;

import com.ds.common.JDSException;
import com.ds.config.TreeListResultModel;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.ApiClassConfig;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.bean.nav.foldingtree.NavFoldingTreeViewBean;
import com.ds.esd.custom.bean.nav.tree.NavTreeViewBean;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.form.pop.PopTreeViewBean;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.tree.ChildTreeViewBean;
import com.ds.esd.custom.tree.CustomTreeViewBean;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.util.TreePageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/dsm/view/config/tree/")
public class ViewChildTreesConfigService {


    @RequestMapping(method = RequestMethod.POST, value = "ViewTreeModule")
    @ModuleAnnotation(dynLoad = true, caption = "获取字段列", imageClass = "bpmfont bpmgongzuoliuxitongpeizhi", index = 0)
    @APIEventAnnotation(bindMenu = CustomMenuItem.loadChild)
    @ResponseBody
    public TreeListResultModel<List<CustomTreeTree>> getViewTreeModule(String domainId, String sourceClassName, String methodName, String viewInstId) {
        TreeListResultModel<List<CustomTreeTree>> result = new TreeListResultModel<>();
        try {
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            ApiClassConfig customESDClassAPIBean = classConfig.getCurrConfig();
            MethodConfig methodAPIBean = customESDClassAPIBean.getMethodByName(methodName);
            CustomTreeViewBean customFormViewBean = null;
            if (methodAPIBean.getView() instanceof NavTreeViewBean) {
                NavTreeViewBean baseViewBean = (NavTreeViewBean) methodAPIBean.getView();
                customFormViewBean = baseViewBean.getTreeViewBean();
            } else if (methodAPIBean.getView() instanceof NavFoldingTreeViewBean) {
                NavFoldingTreeViewBean baseViewBean = (NavFoldingTreeViewBean) methodAPIBean.getView();
                customFormViewBean = baseViewBean.getTreeViewBean();
            } else if (methodAPIBean.getView() instanceof PopTreeViewBean) {
                PopTreeViewBean baseViewBean = (PopTreeViewBean) methodAPIBean.getView();
                customFormViewBean = baseViewBean.getTreeViewBean();
            } else if (methodAPIBean.getView() instanceof CustomTreeViewBean) {
                customFormViewBean = (CustomTreeViewBean) methodAPIBean.getView();
            }

            Set<ChildTreeViewBean> childTreeViewBeans = customFormViewBean.getChildTreeViewBeans();
            result = TreePageUtil.getTreeList(childTreeViewBeans, CustomTreeTree.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return result;
    }

}
